package com.yzy.controller;

import com.yzy.dao.BookDao;
import com.yzy.dao.PatientDao;
import com.yzy.entity.Books;
import com.yzy.entity.Patients;
import com.yzy.utils.Utils;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class PatientBookFindServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext application = request.getServletContext();
        BookDao dao= new BookDao();
        PrintWriter out;
        String getpphone = (String) application.getAttribute("PlonginPhone2");
        String booktime = (String) application.getAttribute("booktime");



//找到预约表的所有
        Utils util = new Utils();
        String sql = "select * from book where pphone=?";
        PreparedStatement ps =util.createStatement(sql);
        List<Books> booklist=new ArrayList<>();
        ResultSet rs =null;
        try {
            ps.setString(1,getpphone);
            rs = ps.executeQuery();
            while (rs.next()){
                String pphone=rs.getString("pphone");
                String did=rs.getString("did");
                String time=rs.getString("time");
                String state=rs.getString("state");
                Books books = new Books(pphone,did,time,state);

                booklist.add(books);

            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            util.close();
        }

//找患者的姓名
        String sql2 ="select pname from patient where pphone=?";
        Utils util2 = new Utils();
        PreparedStatement ps2=util.createStatement(sql2);
        ResultSet rs2=null;
        List<Patients> booklist2=new ArrayList<>();
        try {
            ps2.setString(1,getpphone);
            rs2 =ps2.executeQuery();
            while (rs2.next()){
                String pname=rs2.getString("pname");
                Patients patient=new Patients(pname);
                booklist2.add(patient);
            }


        } catch (SQLException e) {
            e.printStackTrace();
        } finally {

            util.close(rs2);
            util.close();
        }


        response.setContentType("text/html;charset=utf-8");
        out=response.getWriter();

        out.print("<table boder='2' align='center'>");
        out.print("<tr>");
        out.print("<td>患者手机号</td>");
        out.print("<td>患者姓名</td>");
        out.print("<td>医生编号</td>");
        out.print("<td>操作时间</td>");
        out.print("<td>就诊时间</td>");
        out.print("<td>是否就诊</td>");
        out.print("</tr>");
        for(Books book : booklist){
            out.print("<tr>");
            out.print("<td>"+book.getPphone()+"</td>");
            out.print("<td>"+booklist2.get(0).getPname()+"</td>");
            out.print("<td>"+book.getDid()+"</td>");
            out.print("<td>"+book.getTime()+"</td>");
            out.print("<td>"+booktime+"</td>");
            out.print("<td>"+book.getState()+"</td>");
            out.print("</tr>");
        }
        out.print("</table>");

    }
}
